/*
 * @Author: silence
 * @Date: 2022-06-28 14:05:22
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-27 16:38:40
 * @Description: 权限、协议授权弹窗
 */
import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_instance/get_instance.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import '../../../config/AppInfo.dart';
import '../../../pages/Mine/Language/LanguageController.dart';
import '../../../utils/color/ColorUtils.dart';
import '../../../utils/eventBus/eventBus.dart';
import '../../../utils/sputils/SpUtils.dart';
import '../../../utils/string/StringConstant.dart';

import '../../Method/CommonMethod.dart';

class AgreeDialog extends StatefulWidget {
  const AgreeDialog({Key? key}) : super(key: key);

  @override
  State<AgreeDialog> createState() => _AgreeDialogState();
}

class _AgreeDialogState extends State<AgreeDialog> {
  LanguageController _languageController =
      Get.put<LanguageController>(LanguageController());
  bool _isPromptEnglish = true;

  @override
  void initState() {
    super.initState();
    _isPromptEnglish = _languageController.isPromptEnglish();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.card,
      color: Color.fromRGBO(77, 78, 79, 1.0),
      child: Center(
        child: Container(
          width: MediaQuery.of(context).size.width * 0.8,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(6)),
            color: Colors.white,
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              _titleWidget(),
              _bodyWidget(),
              _agreeWidget(),
              _noAgreeWidget(),
            ],
          ),
        ),
      ),
    );
  }

  /**
   * title
   */
  Widget _titleWidget() {
    return Container(
      height: 20.w + 10.w + 26.w,
      child: Center(
        child: SilText(
          _isPromptEnglish ? "Welcome to use ${APP_NAME}" : "欢迎使用${APP_NAME}",
          textDirection: TextDirection.ltr,
          style: TextStyle(
              fontSize: 17.sp,
              color: Colors.black,
              fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  /**
   * 中间内容
   */
  Widget _bodyWidget() {
    Function gotoPage = (int value) {
      if (value == 1) {
        gotoServerTocolPage(context);
      } else {
        gotoProTocolPrivacyPage(context);
      }
    };

    return Padding(
      padding: EdgeInsets.only(left: 22.w, right: 22.w, bottom: 24.w),
      child: Container(
        height: 240.w,
        child: SingleChildScrollView(
          child: RichText(
            textDirection: TextDirection.ltr,
            maxLines: 1000,
            text: TextSpan(children: [
              _bodySpan(
                  _isPromptEnglish
                      ? "We attach great importance to the protection of each user's information and privacy. Please read it carefully before you use the \"${APP_NAME}\" service"
                      : "我们非常重视每一位用户的信息和隐私保护。在您使用“${APP_NAME}”服务前，请仔细阅读",
                  ColorUtils.color_128_128_128),
              _bodySpan(
                  _isPromptEnglish
                      ? "《User agreement and Privacy policy》"
                      : "《用户协议和隐私政策》",
                  ColorUtils.color_theme, click: () {
                gotoPage(1);
              }),
              // _bodySpan(
              //     _isPromptEnglish ? "and" : "和", ColorUtils.color_128_128_128),
              // _bodySpan(_isPromptEnglish ? "《Privacy policy》" : "《隐私政策》",
              //     ColorUtils.color_theme, click: () {
              //   gotoPage(2);
              // }),
              _bodySpan(
                  _isPromptEnglish
                      ? "To understand how we collect, use, share and store information, and how we protect it.\n\n"
                      : "的全部内容，以便了解我们收集、使用、共享、储存信息的情况，以及对信息的保护措施。\n\n",
                  ColorUtils.color_128_128_128),
              _bodySpan(
                  _isPromptEnglish
                      ? "1. When you register, login ${APP_NAME}，We will collect your mobile phone number, email and other information. \n\n2. When you browse, search, we may request to invoke your network permissions for display. \n\n3. When you set your personal information and upload your profile picture, we may apply for permission to call your camera, album, storage, reading and writing, etc., we will ask you whether you are allowed to use the permission when we call for the first time, you have the right to refuse or cancel the authorization. If you agree, please click the button below to accept our service."
                      : "1. 当您注册、登录${APP_NAME}，我们将收集您的手机号、邮箱等信息。\n\n2. 当您浏览、搜索时，我们将可能会申请调用您的网络权限以用于展示。\n\n3. 当您设置个人信息、上传头像时，我们将可能会申请调用您的相机、相册、储存读写等权限，我们将在首次调用时询问您是否允许使用该权限，您有权拒绝或取消授权。\n\n如果您同意请点击下面的按钮以接受我们的服务。",
                  ColorUtils.color_128_128_128),
            ]),
          ),
        ),
      ),
    );
  }

  TextSpan _bodySpan(String txt, Color txtColor, {Function? click}) {
    return TextSpan(
        text: txt,
        style: TextStyle(
          height: 1.5,
          fontSize: 14.sp,
          color: txtColor,
        ),
        recognizer: TapGestureRecognizer()
          ..onTap = () {
            if (click != null) click();
          });
  }

/**
 *  同意按扭
 */
  Widget _agreeWidget() {
    return GestureDetector(
      onTap: () async {
        // Navigator.pop(context);
        await SpUtils.saveBoolData(APP_VERSION_AGREE, true);
        bus.emit(StringConstant.VersionProtocolAgree, true);
      },
      child: Container(
        margin: EdgeInsets.only(left: 24.w, right: 24.w),
        decoration: BoxDecoration(
          color: ColorUtils.color_theme,
          borderRadius: BorderRadius.all(Radius.circular(18)),
        ),
        alignment: Alignment.center,
        height: 36.w,
        child: SilText(_isPromptEnglish ? "Agree" : "同意",
            textDirection: TextDirection.ltr,
            style: TextStyle(
                fontSize: 14.sp,
                color: Colors.white,
                fontWeight: FontWeight.w500)),
      ),
    );
  }

  /**
   * 不同意，退出
   */
  Widget _noAgreeWidget() {
    return GestureDetector(
      onTap: () {
        if (Platform.isAndroid) {
          //SystemNavigator.pop();
          exit(0);
        } else {
          exit(0);
        }
      },
      child: Container(
        margin: EdgeInsets.only(top: 12.w, bottom: 12.w),
        alignment: Alignment.center,
        color: Colors.white,
        height: 36.w,
        child: SilText(_isPromptEnglish ? "Disagree, exit" : "不同意，退出",
            textDirection: TextDirection.ltr,
            style: TextStyle(
                fontSize: 14.sp, color: ColorUtils.color_128_128_128)),
      ),
    );
  }
}
